﻿
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using GDFlacTool.Properties;

namespace GDFlacTool.forms
{
    public partial class FormAlbum : Form
    {
        private readonly DateTime _recordingDate;
        private string _albumText = string.Empty;
        private string _catalog = string.Empty;

        public FormAlbum(string catalog)
        {
            InitializeComponent();

            _recordingDate = AlbumHelperSettings.Default.RecordingDate;

            _catalog = catalog;
        }

        public string AlbumText
        {
            get { return _albumText; }
        }

        public string Catalog
        {
            get { return _catalog; }
        }

        private void FormAlbum_Load(object sender, EventArgs e)
        {
            StringCollection recordingTypes = MainSettings.Default.RecordingType;

            foreach (string artist in recordingTypes)
            {
                cboRecordingType.Items.Add(artist);
            }

            StringCollection states = AlbumHelperSettings.Default.USStates;

            foreach (string state in states)
            {
                cboStates.Items.Add(state);
            }
            
            dtpRecordingDate.Checked = false;

            dtpRecordingDate.Value = _recordingDate;
            

            int index1 = cboRecordingType.Items.IndexOf(AlbumHelperSettings.Default.RecordingType);

            if (index1 >= 0)
            {
                cboRecordingType.SelectedIndex = index1;
            }

            int index2 = AlbumHelperSettings.Default.State;

            if (index2 >= 0)
            {
                cboStates.SelectedIndex = index2;
            }

            txtCity.Text = AlbumHelperSettings.Default.City;

            txtVenue.Text = AlbumHelperSettings.Default.Venue;

            if(!string.IsNullOrEmpty(_catalog))
            {
                txtShn.Text = _catalog;
            }
            else
            {
                txtShn.Text = AlbumHelperSettings.Default.SHNId;
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            _albumText = txtPreview.Text;
        }

        private void cboRecordingType_TextChanged(object sender, EventArgs e)
        {
            CreatePreviewText();
        }

        private void dtpRecordingDate_ValueChanged(object sender, EventArgs e)
        {
            CreatePreviewText();
        }

        private void txtVenue_TextChanged(object sender, EventArgs e)
        {
            CreatePreviewText();
        }

        private void txtCity_TextChanged(object sender, EventArgs e)
        {
            CreatePreviewText();
        }

        private void cboStates_TextChanged(object sender, EventArgs e)
        {
            CreatePreviewText();
        }

        private void txtShn_TextChanged(object sender, EventArgs e)
        {
            CreatePreviewText();

            _catalog = txtShn.Text;
        }

        private void CreatePreviewText()
        {
            string recordingType = string.Empty;
            string recordingDate = string.Empty;
            string venue = string.Empty;
            string city = string.Empty;
            string state = string.Empty;
            string shnid = string.Empty;

            if(!string.IsNullOrEmpty(cboRecordingType.Text))
            {
                recordingType = string.Format("{0}:", cboRecordingType.Text);
            }

            if(dtpRecordingDate.Checked)
            {
                recordingDate = string.Format("{0:MM}-{1:dd}-{2:yy}", dtpRecordingDate.Value, dtpRecordingDate.Value, dtpRecordingDate.Value);
            }

            if(!string.IsNullOrEmpty(txtVenue.Text))
            {
                venue = string.Format("{0},", txtVenue.Text);
            }

            if(!string.IsNullOrEmpty(txtCity.Text))
            {
                city = string.Format("{0},", txtCity.Text);
            }

            if(!string.IsNullOrEmpty(cboStates.Text) && cboStates.Text.Length > 1)
            {
                state = cboStates.Text.Substring(0, 2).ToUpper();
            }

            if(!string.IsNullOrEmpty(txtShn.Text))
            {
                shnid = string.Format("({0})", txtShn.Text);
            }

            string preview = string.Format("{0} {1} {2} {3} {4} {5}", recordingType, recordingDate, venue, city, state, shnid);

            if(!string.IsNullOrEmpty(preview))
            {
                preview = preview.Replace("  ", " ");

                preview = preview.Replace(",,", ",");

                preview = preview.Trim();
                
                txtPreview.Text = preview;
            }
            else
            {
                txtPreview.Text = string.Empty;
            }
        }

        private void FormAlbum_FormClosing(object sender, FormClosingEventArgs e)
        {
            AlbumHelperSettings.Default.RecordingType = cboRecordingType.Text;
            AlbumHelperSettings.Default.State = cboStates.SelectedIndex;
            AlbumHelperSettings.Default.City = txtCity.Text;
            AlbumHelperSettings.Default.Venue = txtVenue.Text;
            AlbumHelperSettings.Default.SHNId = txtShn.Text;
            AlbumHelperSettings.Default.RecordingDate = dtpRecordingDate.Value;

            AlbumHelperSettings.Default.Save();
        }
    }
}
